package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.Categories;
import com.example.mall.domain.ProductInventory;
import com.example.mall.dto.CategoriesDto;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.mapper.CategoriesMapper;
import com.example.mall.query.CategoriesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.service.ICategoriesService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper, Categories> implements ICategoriesService {

    @Autowired
    private CategoriesMapper categoriesMapper;

    @Override
    public PageDTO<CategoriesDto> queryPage(CategoriesQuery categoriesQuery) {
        if (categoriesQuery == null) {
            categoriesQuery = new CategoriesQuery();
        }
        Page<Categories> page = categoriesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<Categories> cartPage = lambdaQuery()
                .like(categoriesQuery.getCategoryId()!= null, Categories::getCategoryId, categoriesQuery.getCategoryId())
                .like(categoriesQuery.getDescription()!= null, Categories::getDescription, categoriesQuery.getDescription())
                .like(categoriesQuery.getIsActive()!= null, Categories::getIsActive, categoriesQuery.getIsActive())
                .like(categoriesQuery.getName()!= null, Categories::getName, categoriesQuery.getName())
                .like(categoriesQuery.getParentId()!= null, Categories::getParentId, categoriesQuery.getParentId())
                .orderByDesc(Categories::getUpateDate)
                .page(page);
        return PageDTO.of(cartPage, CategoriesDto.class);
    }

    @Override
    public Categories queryCategoriesById(Long categoryId) {
        AssertUtil.isNotNull(categoryId, "分类ID不能为空");
        return categoriesMapper.selectById(categoryId);
    }

    @Override
    public List<Categories> queryCategoriesList() {
        return this.list();
    }

    @Override
    public void insert(CategoriesDto categoriesDto) {
        Categories categories = new Categories();
        CopyUtils.copyProperties(categoriesDto, categories);
        categories.setCategoryId(CodeGenerateUtils.generateUniqueId());
        categories.setIsActive(BaseConstants.EFFECTIVE_STATUS);
        categories.setCreateDate(new Date());
        categories.setUpateDate(new Date());
        categoriesMapper.insert(categories);
    }

    @Override
    public void update(CategoriesDto categoriesDto) {
        AssertUtil.isNotNull(categoriesDto.getCategoryId(), "分类ID不能为空");
        Categories categories = categoriesMapper.selectById(categoriesDto.getCategoryId());
        AssertUtil.isNotNull(categories, "分类不存在");
        CopyUtils.copyProperties(categoriesDto, categories);
        categories.setUpateDate(new Date());
        categoriesMapper.updateById(categories);
    }

    @Override
    public void delete(Long categoryId) {
        AssertUtil.isNotNull(categoryId, "分类ID不能为空");
        Categories categories = categoriesMapper.selectById(categoryId);
        AssertUtil.isNotNull(categories, "分类不存在");
        categories.setIsActive(BaseConstants.LOSE_STATUS);
        categories.setUpateDate(new Date());
        categoriesMapper.updateById(categories);
    }

    @Override
    public void deleteBatch(List<Long> categoryIds) {
        AssertUtil.isNotNull(categoryIds, "分类ID列表不能为空");
        List<Categories> categories = categoriesMapper.selectBatchIds(categoryIds);
        AssertUtil.isNotNull(categories, "分类不存在");
        for (Categories category : categories) {
            category.setIsActive(BaseConstants.LOSE_STATUS);
            category.setUpateDate(new Date());
            categoriesMapper.updateById(category);
        }
    }
}
